package com.android.dopewars;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.Vector;

public class Global {
	public static long current_dealer_id_;
	
	public static final int RANDOM_EVENT_NEW_COAT = 0;
	public static final int RANDOM_EVENT_GUN = 1;
	public static final int RANDOM_EVENT_MUGGED = 2;
	public static final int RANDOM_EVENT_FIGHT = 3;
	public static final int RANDOM_EVENT_STORE = 4;
	public static final int RANDOM_EVENT_NONE = -1;
	
	// Game-level data, used for passing things around between views
	public static Vector<Dealer> dealers_ = new Vector<Dealer>();
	public static Dealer dealer_;
	
	// Store data
	public static boolean[] gun_available;
	public static boolean[] coat_available;
	
	public static void unstockStore() {
		for (int i = 0; i < gun_available.length; ++i) {
			gun_available[i] = false;
		}
		for (int i = 0; i < coat_available.length; ++i) {
			coat_available[i] = false;
		}
	}
	
	public static void stockStore() {
		boolean something_available = false;
		for (int i = 0; i < gun_available.length; ++i) {
			if (Global.rand_gen_.nextDouble() < 0.25) {
				gun_available[i] = true;
				something_available = true;
			}
		}
		for (int i = 0; i < coat_available.length; ++i) {
			if (Global.rand_gen_.nextDouble() < (2.0 / available_bag_sizes_.elementAt(i))) {
				coat_available[i] = true;
				something_available = true;
				available_bag_prices_.setElementAt((int)(800.0 + (400.0 * Global.rand_gen_.nextDouble()) * (available_bag_sizes_.elementAt(i) - 9.0) *
					((dealer_.total_space() - 95) / 5.0)), i);
			}
		}
		if (!something_available) {
			// pick one random thing
			int item = Global.rand_gen_.nextInt(gun_available.length + coat_available.length);
			if (item < gun_available.length) {
				gun_available[item] = true;
			} else {
				coat_available[item - gun_available.length] = true;
				available_bag_prices_.setElementAt((int)(800 + (400.0 * Global.rand_gen_.nextDouble()) *
						(available_bag_sizes_.elementAt(item - gun_available.length) - 9.0) *
						((dealer_.total_space() - 95) / 5.0)), item - gun_available.length);
			}
		}
	}
	
	public static boolean showStore() {
		for (int i = 0; i < gun_available.length; ++i) {
			if (gun_available[i]) {
				return true;
			}
		}
		for (int i = 0; i < coat_available.length; ++i) {
			if (coat_available[i]) {
				return true;
			}
		}
		return false;
	}
	
	// Much more transient data, indicative of bad coding style, but fuck it
	public static int days_left_ = 60;
	public static int total_days_ = 60;
	public static boolean first_day_ = true;
	public static int location_ = 0;
	public static Vector<Integer> days_since_location_ = new Vector<Integer>();
	public static Vector<String> ui_messages_ = new Vector<String>();
	public static Vector<Drug> current_drugs_ = new Vector<Drug>();
	public static int buy_sell_drug_ = 0;
	public static int maximum_loan_amount_ = 100000;
	
	// User event back-and-forth
	public static String new_user_event_player_ = "";
	public static String new_user_event_ = "";
	
	// High score view back-and-forth
	public static String high_score_name_ = "";
	public static String high_score_comment_ = "";
	public static String high_score_mode_ = "";
	public static int high_score_ = 0;
	
	// High score list back-and-forth
	public static Vector<String> high_score_list_names_ = new Vector<String>();
	public static Vector<String> high_score_list_scores_ = new Vector<String>();
	public static Vector<String> high_score_list_dates_ = new Vector<String>();
	public static Vector<String> high_score_list_comments_ = new Vector<String>();
	public static int high_score_list_row_to_highlight_ = -1;
	
	public static Vector<Drug> available_drugs_ = new Vector<Drug>();
	public static void setupDrugs() {
    	available_drugs_.clear();
    	available_drugs_.add(new Drug("Oatmeal", 0.5, 2700, 500, 1700, true, false, 4.0));
    	available_drugs_.add(new Drug("Peanut Butter", 0.5, 22000, 5000, 7000, false, true, 4.0));
    	available_drugs_.add(new Drug("Chocolate Chip", 0.5, 880, 250, 400, true, false, 4.0));
    	available_drugs_.add(new Drug("Sugar", 0.5, 9250, 2000, 3750, false, true, 4.0));
    	available_drugs_.add(new Drug("Snickerdoodle", 0.5, 35, 10, 15, true, false, 4.0));
    	available_drugs_.add(new Drug("Rasin", 0.5, 3000, 700, 1500, false, false, 4.0));
    	available_drugs_.add(new Drug("Double Chocolate Chunk", 0.5, 900, 250, 360, false, true, 4.0));
    	available_drugs_.add(new Drug("Macaroon", 0.5, 1750, 400, 750, false, false, 4.0));
    	available_drugs_.add(new Drug("Christmas", 0.5, 460, 150, 240, false, false, 4.0));
    	available_drugs_.add(new Drug("White Chocolate", 0.5, 950, 300, 350, false, false, 4.0));
    	available_drugs_.add(new Drug("Mint Chocolate Chip", 0.5, 175, 30, 75, false, true, 4.0));
    	available_drugs_.add(new Drug("Butterscotch", 0.5, 600, 200, 290, true, false, 4.0));
    }
    public static Vector<Gun> available_guns_ = new Vector<Gun>();
    public static void setupGuns() {
    	available_guns_.clear();
    	available_guns_.add(new Gun("Baretta", 3000, 4, 5));
    	available_guns_.add(new Gun(".38 Special", 3500, 4, 9));
    	available_guns_.add(new Gun("Ruger", 2900, 4, 4));
    	available_guns_.add(new Gun("Saturday Night Special", 3100, 4, 7));
    	gun_available = new boolean[available_guns_.size()];
    	for (int i = 0; i < available_guns_.size(); ++i) {
    		gun_available[i] = false;
    	}
    }
    
    public static Vector<String> available_bag_names_ = new Vector<String>();
    public static Vector<Integer> available_bag_sizes_ = new Vector<Integer>();
    public static Vector<Integer> available_bag_prices_ = new Vector<Integer>();
    public static void setupBags() {
    	available_bag_names_.clear();
    	available_bag_sizes_.clear();
    	available_bag_names_.add("Bigger truck");
    	available_bag_sizes_.add(10);
    	available_bag_prices_.add(0);
    	available_bag_names_.add("Way bigger truck");
    	available_bag_sizes_.add(20);
    	available_bag_prices_.add(0);
    	available_bag_names_.add("Fanny pack");
    	available_bag_sizes_.add(10);
    	available_bag_prices_.add(0);
    	available_bag_names_.add("Bigger backpack");
    	available_bag_sizes_.add(20);
    	available_bag_prices_.add(0);
    	available_bag_names_.add("Giant Santa's toy sack");
    	available_bag_sizes_.add(30);
    	available_bag_prices_.add(0);
    	coat_available = new boolean[available_bag_names_.size()];
    	for (int i = 0; i < available_bag_names_.size(); ++i) {
    		coat_available[i] = false;
    	}
    }
    public static Vector<String> songs_ = new Vector<String>();
    public static void setupSongs() {
    	songs_.clear();
    	songs_.add("C is for Cookie by Cookie Monster");	
    	songs_.add("Cookie Jar by Jack Johnson");
    	songs_.add("Milk and Cookies by REM");
    	songs_.add("Cookie Core by Lunachicks");
    	songs_.add("Cookies by Keller Williams");
    	songs_.add("Pour some Sugar on Me by Def Lepard");
    }
    public static Vector<String> random_messages_ = new Vector<String>();
    public static void setupRandomMessages() {
    	random_messages_.clear();
		String user_messages_string = "http://dopewars-highscores.appspot.com/get_user_events";
		int response_code = -1;
		try {
    		HttpURLConnection conn = (HttpURLConnection) (new URL(user_messages_string)).openConnection();
    		conn.setDoInput(true);
    		conn.setDoOutput(true);
    		conn.connect();
    		response_code = conn.getResponseCode();
    		if (response_code != -1) {
	    	    String string;
	    	    if (conn.getInputStream() != null) {
	    	        BufferedReader reader = new BufferedReader(new InputStreamReader(
	    	        		conn.getInputStream()));
	    	        while (null != (string = reader.readLine())) {
	    	        	random_messages_.add(string);
	    	        }
	    	    }
	    	    conn.disconnect();
    		}
		} catch (IOException e) {
		}
		
		if ((response_code == -1) || (random_messages_.size() == 0)) {
	    	random_messages_.add("Wouldn't it be funny if everyone suddenly ate a cookie at once?");
	    	random_messages_.add("The Pope was once a cookie addict, you know.");
	    	random_messages_.add("I'll bet you have some really interesting dreams.");
	    	random_messages_.add("So I think I'm going to Cookiefest this year.");
	    	random_messages_.add("Son, you need a new cookie.");
	    	random_messages_.add("I think it's wonderful what they're doing with sugar these days.");
	    	random_messages_.add("I wasn't always a cookie addict, you know.");
	    	random_messages_.add("Does your mother know you're a cookie dealer?");
	    	
		}
    }
    
    // Fight stuff
    public static String officer_name_ = "Baker Jones";
    public static int officer_health_ = 5;
    public static int officer_firepower_ = 5;
    public static int officer_num_deputies_ = 1;
    
    // Random number generator!
    public static Random rand_gen_ = new Random();
    public static int randomEvent() {
    	if (rand_gen_.nextDouble() < 0.27) {
    		double event_chooser = rand_gen_.nextDouble();
    		if (days_left_ > 0.5 * total_days_) {
    			if (event_chooser < 0.75) {
    				return RANDOM_EVENT_STORE;
    			} else if (event_chooser < 0.85) {
    				return RANDOM_EVENT_MUGGED;
    			} else {
    				return RANDOM_EVENT_FIGHT;
    			}
    		} else {
    			if (event_chooser < 0.40) {
    				return RANDOM_EVENT_STORE;
    			} else if (event_chooser < 0.60) {
    				return RANDOM_EVENT_MUGGED;
    			} else {
    				return RANDOM_EVENT_FIGHT;
    			}
    		}
    	}
    	return RANDOM_EVENT_NONE;
    }
    
    public static String connectionToString(InputStream inputStream) throws IOException {
		StringBuilder outputBuilder = new StringBuilder();
	    String string;
	    if (inputStream != null) {
	        BufferedReader reader = new BufferedReader(new InputStreamReader(
	                inputStream, "UTF-8"));
	        while (null != (string = reader.readLine())) {
	            outputBuilder.append(string).append('\n');
	        }
	    }
	    return outputBuilder.toString();
    }
}
